import multiprocessing
import threading

import time
import os


def func(i):
    time.sleep(3)


def stat_thread():
    print("当前同时存在的线程个数：", len(threading.enumerate()))
def task1(name,outer_name):
    # 线程锁，由子进程的主线程申请到了（注意：只是在一个进程中只有一个线程能获得锁）
    print(f"{name} before....")
    print("当前进程名：", multiprocessing.current_process().name)
    print("当前进程号：", os.getpid())
    print("当前进程名：", multiprocessing.parent_process().name)
    print("当前进程的父进号：", os.getppid())
    stat_thread()
    # stat_thread()
    #time.sleep(0.1)
    print(f"{name} after....")
    # lock.release() 放上，放下一样
    for i in range(10):
        t = threading.Thread(target=func,args=(i,))
        t.start()
    stat_thread()


if __name__  == '__main__':
    multiprocessing.set_start_method("fork")
    outer_name = []
    outer_name.append(111)

    p1 = multiprocessing.Process(target=task1, args=("xx", outer_name))
    p2 = multiprocessing.Process(target=task1, args=("xxxx", outer_name))
    # 没有set方法，直接改属性
    p1.name = "p1"
    p2.name = "p2"
    # p2.daemon = True
    # p1.daemon = False
    p1.start()
    p2.start()
    # p1.join()
    # p2.join()
    print("end")